package me.lc.superDown.layer;

import me.lc.superDown.R;
import me.lc.superDown.core.GameManager;
import me.lc.superDown.core.GameSystem;
import me.lc.superDown.scene.GameScene;
import me.lc.superDown.scene.HelpScene;
import me.lc.superDown.util.AudioHelper;
import android.app.Activity;

import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Scene;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.transitions.TransitionScene;
import com.wiyun.engine.transitions.TurnOffTilesTransition;
import com.wiyun.engine.types.WYPoint;
import com.wiyun.game.WiGame;

/**
 * 主菜单层
 * 
 * @author Administrator
 * 
 */
public class MainLayer extends Layer {
	Button btn_newgame;
	Button btn_top;
	Button btn_help;
	Button btn_audioOn;
	Button btn_audioOff;

	Sprite bg;

	public MainLayer() {
		bg = Sprite.make(GameSystem.tex_mainbg);
		bg.setAnchor(0, 0);
		bg.setPosition(WYPoint.make(0, 0));
		bg.setScaleX(GameSystem.Screen_Width / GameSystem.tex_mainbg.getWidth());
		bg.setScaleY(GameSystem.Screen_Height
				/ GameSystem.tex_mainbg.getHeight());
		addChild(bg);

		// 添加按钮
		btn_newgame = Button.make(R.drawable.button_newgame,
				R.drawable.button_newgame_selected, this,
				"onNewGameButtonClicked");
		btn_newgame.setPosition(GameSystem.Screen_Width / 2, getButtonY(180));
		addChild(btn_newgame);

		btn_top = Button.make(R.drawable.button_top,
				R.drawable.button_top_selected, this, "onTopButtonClicked");
		btn_top.setPosition(GameSystem.Screen_Width / 2, getButtonY(225));
		addChild(btn_top);

		btn_help = Button.make(R.drawable.button_help,
				R.drawable.button_help_selected, this, "onHelpButtonClicked");
		btn_help.setPosition(GameSystem.Screen_Width / 2, getButtonY(270));
		addChild(btn_help);

		btn_audioOn = Button.make(R.drawable.button_audioon,
				R.drawable.button_audioon_selected, this,
				"onAudioOnButtonClicked");
		btn_audioOn.setPosition(GameSystem.Screen_Width / 2, getButtonY(315));
		addChild(btn_audioOn);

		btn_audioOff = Button.make(R.drawable.button_audiooff,
				R.drawable.button_audiooff_selected, this,
				"onAudioOffButtonClicked");
		btn_audioOff.setPosition(GameSystem.Screen_Width / 2, getButtonY(315));
		btn_audioOff.setVisible(false);
		addChild(btn_audioOff);

		initAudioButton();
	}

	private void initAudioButton() {
		if (!GameManager.instance.isAudioOn) {
			btn_audioOff.setVisible(true);
			btn_audioOn.setVisible(false);
		}
	}

	public void onAudioOffButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				btn_audioOff.setVisible(false);
				btn_audioOn.setVisible(true);
				GameManager.instance.isAudioOn = true;
				GameManager.instance.saveAudioConfig();
				AudioHelper.click();
				AudioHelper.playMusicInGame();
			}
		});
	}

	public void onAudioOnButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				btn_audioOff.setVisible(true);
				btn_audioOn.setVisible(false);
				GameManager.instance.isAudioOn = false;
				GameManager.instance.saveAudioConfig();
				AudioHelper.click();
				AudioHelper.stopMusic();
			}
		});
	}

	public void onHelpButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.click();
				// 显示帮助界面
				Director.getInstance().pushScene(
						getTransition(HelpScene.make()));
			}
		});
	}

	protected TransitionScene getTransition(Scene scene) {
		return (TransitionScene) TurnOffTilesTransition.make(1f, scene)
				.autoRelease();
	}

	public void onTopButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.click();
				WiGame.openLeaderboard("dbadeff37e6bc0a6");
			}
		});
	}

	public void onNewGameButtonClicked() {
		((Activity) GameSystem.AndroidContext).runOnUiThread(new Runnable() {
			public void run() {
				AudioHelper.click();
				GameManager.instance.startNewGame(GameScene.make().layer);
			}
		});
	}

	private float getButtonY(float y) {
		return ((480 - y) / 480) * GameSystem.Screen_Height;
	}

}
